package cc.telecomdigital.tdstock.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import cc.telecomdigital.tdstock.CommonDefn;
import cc.telecomdigital.tdstock.DefaultClickHandler;
import cc.telecomdigital.tdstock.ITDLApplication;
import cc.telecomdigital.tdstock.R;
import cc.telecomdigital.tdstock.Framework.UniqueItem.TDStockLog;
import cc.telecomdigital.tdstock.activity.groups.MonitorGroup;
import cc.telecomdigital.tdstock.activity.groups.monitor.PortfolioActivity;
import cc.telecomdigital.tdstock.activity.groups.monitor.StockDiaryActivity;

public class MonitorListItemAdapter extends BaseAdapter
{
	private static final String		TAG	= MonitorListItemAdapter.class
											.getSimpleName();
	private LayoutInflater			minflater;
	private List<MonitorViewHolder>	list;
	private SharedPreferences				_prefsStockCode;

	public MonitorListItemAdapter(LayoutInflater inflater,
			List<MonitorViewHolder> list)
	{
		super();
		this.minflater = inflater;
		if(this.minflater.getContext() instanceof PortfolioActivity) {
			TDStockLog.d(TAG, "the context is PortfolioActivity!");
		}
		
		this.list = list;
		_prefsStockCode = this.minflater.getContext().getSharedPreferences(
				CommonDefn.SYSTEM_CURRENT_SETTING,
				Context.MODE_PRIVATE);
	}

	@Override
	public int getCount()
	{
		return this.list.size();
	}

	@Override
	public Object getItem(int position)
	{
		return position < this.list.size() ? this.list.get(position) : null;
	}

	@Override
	public long getItemId(int position)
	{
		return position < this.list.size() ? this.list.get(position).id : -1l;
	}

	//Base on 320 design
	private int	item1_width	= 80;
	private int	item2_width	= 0;
	private int	item3_width	= 0;

	public void SetItemWidth(int iItem1Width, int iItem2Width, int iItem3Width)
	{
		item1_width = iItem1Width;
		item2_width = iItem2Width;
		item3_width = iItem3Width;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		//View removeButton;
		//View extraArrowButton;

		/*if (this.list == null || this.list.isEmpty()
				|| this.list.size() < (position + 1))
		{
			TDStockLog.d(TAG, " didn't update the row for its size is two  less than the old!" ) ;
			return convertView == null ? minflater.inflate(
					R.layout.monitor_list_item, null) : convertView;
		}*/

		//extraArrowButton = null;
		//removeButton = null;
		try
		{
			
			TDStockLog.d(TAG, "list size: "  + list.size()) ;
			final MonitorViewHolder monitorHolder = (convertView == null ||  convertView.getTag() == null) ? new MonitorViewHolder()
					: (MonitorViewHolder) convertView.getTag();
			if (convertView == null)
			{
				convertView = minflater.inflate(R.layout.monitor_list_item,
						null);
				monitorHolder.itemCode = (TextView) convertView
						.findViewById(R.id.monitor_item_code);
				monitorHolder.itemName = (TextView) convertView
						.findViewById(R.id.monitor_item_name);
				monitorHolder.itemValue = (TextView) convertView
						.findViewById(R.id.monitor_item_value);
				monitorHolder.itemNet = (TextView) convertView
						.findViewById(R.id.monitor_item_net);
				monitorHolder.itemNetPercentage = (TextView) convertView
						.findViewById(R.id.monitor_item_netpercentage);
				//monitorHolder.addIcon  = (ViewStub)convertView.findViewById(R.id.stub_edit);
				//monitorHolder.addBtn   = (Button)convertView.findViewById(R.id.monitor_edit);
				monitorHolder.dragIcon = (ImageView) convertView
						.findViewById(R.id.icon);
				monitorHolder.isEditable = this.list.get(position).isEditable;
				
				//removeButton = convertView.findViewById(R.id.remove_icon);
				monitorHolder.removeIcon =   (ImageButton)convertView.findViewById(R.id.remove_icon);
				monitorHolder.delBtn = (ImageView)convertView.findViewById(R.id.icon_confirm_del);
				
				monitorHolder.edtBtn =  (ImageButton) convertView.findViewById(R.id.monitor_item_add);
				//extraArrowButton =  monitorHolder.edtBtn;
				
			/*	monitorHolder.edtBtn.setOnClickListener(  
							new ForwardToUserDiary(
									minflater.getContext(),position));*/
				
				

				if (item1_width != 0)
					monitorHolder.itemName.setWidth(item1_width);

				if (item2_width != 0)
					monitorHolder.itemValue.setWidth(item2_width);

				if (item3_width != 0)
					monitorHolder.itemNetPercentage.setWidth(item3_width);

				convertView.setTag(monitorHolder);
				
				TDStockLog.d(TAG, "generated a new  value for " + this.list.get(position) .itemCode.getText().toString()) ;
			}
	
			monitorHolder.isEditable = this.list.get(position).isEditable;
			monitorHolder.isTop20orSector = this.list.get(position)	.isTop20orSector;

			
			monitorHolder.itemCode.setText(this.list.get(position).itemCode.getText());
			monitorHolder.itemName.setText(this.list.get(position).itemName.getText());
			monitorHolder.itemValue.setText(this.list.get(position).itemValue.getText());
			monitorHolder.itemNet.setText(this.list.get(position).itemNet.getText());
			monitorHolder.itemNetPercentage.setText(this.list.get(position).itemNetPercentage.getText());

			if(this.list.get(position).itemCode.getText().toString().length() > 5)
			{
				monitorHolder.itemCode.setTextSize(11);
			}
			else {
				monitorHolder.itemCode.setTextSize(14);
			}
			//monitorHolder.itemNet.setTextColor(this.list.get(position).itemNet.getText().toString().indexOf("-") >= 0 ? Color.RED : Color.GREEN);
			//monitorHolder.itemNetPercentage.setTextColor(this.list.get(position).itemNet.getText().toString().indexOf("-") >= 0 ? Color.RED : Color.GREEN);
			monitorHolder.itemNet.setTextColor(this.list.get(position).itemNet.getText().toString().indexOf("-") >= 0 && !this.list.get(position).itemNet.getText().toString().endsWith("-") ? Color.RED : CommonDefn.FOREST_GREEN);
			monitorHolder.itemNetPercentage.setTextColor(this.list.get(position).itemNet.getText().toString().indexOf("-") >= 0 && !this.list.get(position).itemNet.getText().toString().endsWith("-") ? Color.RED : CommonDefn.FOREST_GREEN);

			if (monitorHolder.dragIcon != null)
			{
				(monitorHolder.dragIcon)
						.setVisibility(monitorHolder.isEditable ? View.VISIBLE
								: View.GONE);
				//monitorHolder.dragIcon.bringToFront();
			}

			if (monitorHolder.isEditable)
			{
				/*if (removeButton != null)
					removeButton.setVisibility(View.VISIBLE);*/
				if (monitorHolder.removeIcon != null)
					monitorHolder.removeIcon.setVisibility(View.VISIBLE);
				
				/*if (extraArrowButton != null)
					extraArrowButton.setVisibility(View.GONE);*/
				
				if (monitorHolder.edtBtn != null)
					  monitorHolder.edtBtn.setVisibility(View.GONE);
				
			}
			  else
			  {
				  if (monitorHolder.removeIcon != null)
						monitorHolder.removeIcon.setVisibility(View.GONE);
				  
				  
				  
				  if (monitorHolder.edtBtn != null && monitorHolder.isTop20orSector)
					  monitorHolder.edtBtn.setVisibility(View.GONE);
				  else if(monitorHolder.edtBtn != null)
					  monitorHolder.edtBtn.setVisibility(View.VISIBLE);
				  
				  if(monitorHolder.delBtn!=null)
						monitorHolder.delBtn.setVisibility(View.GONE);

			  }
			
			
			TDStockLog.d(TAG, "isEditable=" +  (monitorHolder.isEditable ? "true" : "false" ) ) ;
			TDStockLog.d(TAG, "isCustomerSubscribed=" +  (monitorHolder.isTop20orSector ? "false" : "true" ) ) ;
			
			monitorHolder.edtBtn.setOnClickListener(  
					new ForwardToUserDiary(
							minflater.getContext(),position));

		}
		catch (Exception e)
		{
			TDStockLog.e(TAG,
					e.getMessage() == null ? " unkown exception happened!"
							: e.getMessage());
			e.printStackTrace();
		}
		
		return convertView;
	}
	
	

	private class ForwardToUserDiary extends DefaultClickHandler
	{
		private int	_position;

		ForwardToUserDiary(Context context, int position)
		{
			super(context);
			_position = position;
		}

		@Override
		protected void onDefaultAction()
		{
			if("".equals(list.get(_position).itemName.getText().toString()))
			{
				return;
			}
			((ITDLApplication) minflater.getContext().getApplicationContext()).putString(
					CommonDefn.STOCK_DIARY_EDITING_ITEMCODE, list
							.get(_position).itemCode.getText()
							.toString());
			((ITDLApplication) minflater.getContext().getApplicationContext()).putString(
					CommonDefn.STOCK_DIARY_EDITING_ITEMNAME, list
							.get(_position).itemName.getText()
							.toString());
			((ITDLApplication) minflater.getContext().getApplicationContext()).putString( 
					CommonDefn.STOCK_DIARY_EDITING_CURRENT_PRICE,
					list.get(_position).itemValue.getText()
							.toString());
			
		/*	Toast.makeText(minflater.getContext(), String.format("pos=%d & name=%s,code=%s", _position,   list
					.get(_position).itemName.getText()
					.toString(),  list
					.get(_position).itemCode.getText()
					.toString()) , 2500).show();*/

			/*Intent intent =new Intent(getApplicationContext(),StockDiaryActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
			startActivity(intent);*/

			Intent intent = new Intent(minflater.getContext().getApplicationContext(),
					StockDiaryActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_NO_USER_ACTION);
		/*	View viewIntent = MonitorGroup.group.getLocalActivityManager()
					.startActivity(
							StockDiaryActivity.class.getSimpleName(),
							intent).getDecorView();
			MonitorGroup.group.setContentView(viewIntent); // modify by ouyangzhihui 2011-05-16
*/		
			//if(minflater.getContext() instanceof PortfolioActivity) {
				MonitorGroup.GetActivityGroupHostControl().ActivityForwardSwitch(MonitorGroup.group.getCurrentActivity() , StockDiaryActivity.class, intent);
			//}
			
			//SwitchForwardActivity(StockDiaryActivity.class, intent);
			

			
		
		}

	}
	
	
	
	
	
}
